import request from '@/router/axios';
import Mock from 'mockjs';

let allList = [
    {
        id: 1,             //分类id
        keywords: "服装",   //关键词
        level: 0,           //等级  0 一级分类  1 二级分类
        name: "服装",       //名称     
        parentId: 0,        //父级分类id
        productCount: 100,  //数量
        productUnit: "件",   //单位
        showStatus: 1,       //是否显示 0 不显示  1显示
        children:[]
    },
    {
        id: 7,
        keywords: "外套",
        level: 1,
        name: "外套",
        parentId: 1,
        productCount: 190,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 8,
        keywords: "T恤",
        level: 1,
        name: "T恤",
        parentId: 1,
        productCount: 150,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 9,
        keywords: "休闲裤",
        level: 1,
        name: "休闲裤",
        parentId: 1,
        productCount: 230,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 10,
        keywords: "牛仔裤",
        level: 1,
        name: "牛仔裤",
        parentId: 1,
        productCount: 90,
        productUnit: "件",
        showStatus: 0 ,
        children:[]
    },
    {
        id: 11,
        keywords: "寸衫",
        level: 1,
        name: "寸衫",
        parentId: 1,
        productCount: 590,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 2,             //分类id
        keywords: "手机数码",   //关键词
        level: 0,           //等级  0 一级分类  1 二级分类
        name: "手机数码",       //名称     
        parentId: 0,        //父级分类id
        productCount: 500,  //数量
        productUnit: "件",   //单位
        showStatus: 0,       //是否显示 0 不显示  1显示
        children:[]
    },
    {
        id: 19,
        keywords: "手机通讯",
        level: 1,
        name: "手机通讯",
        parentId: 2,
        productCount: 890,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 30,
        keywords: "手机配件",
        level: 1,
        name: "手机配件",
        parentId: 2,
        productCount: 50,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 31,
        keywords: "摄影摄像",
        level: 1,
        name: "摄影摄像",
        parentId: 2,
        productCount: 30,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 32,
        keywords: "影音娱乐",
        level: 1,
        name: "影音娱乐",
        parentId: 2,
        productCount: 190,
        productUnit: "件",
        showStatus: 0 ,
        children:[]
    },
    {
        id: 33,
        keywords: "数码配件",
        level: 1,
        name: "数码配件",
        parentId: 2,
        productCount: 387,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 3,             //分类id
        keywords: "家用电器",   //关键词
        level: 0,           //等级  0 一级分类  1 二级分类
        name: "家用电器",       //名称     
        parentId: 0,        //父级分类id
        productCount: 360,  //数量
        productUnit: "件",   //单位
        showStatus: 1,       //是否显示 0 不显示  1显示
        children:[]
    },
    {
        id: 35,
        keywords: "电视",
        level: 1,
        name: "电视",
        parentId: 3,
        productCount: 190,
        productUnit: "件",
        showStatus: 0 ,
        children:[]
    },
    {
        id: 36,
        keywords: "空调",
        level: 1,
        name: "空调",
        parentId: 3,
        productCount: 650,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 37,
        keywords: "洗衣机",
        level: 1,
        name: "洗衣机",
        parentId: 3,
        productCount: 130,
        productUnit: "件",
        showStatus: 0 ,
        children:[]
    },
    {
        id: 38,
        keywords: "冰箱",
        level: 1,
        name: "冰箱",
        parentId: 3,
        productCount: 190,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 39,
        keywords: "厨卫大电",
        level: 1,
        name: "厨卫大电",
        parentId: 3,
        productCount: 180,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 4,             //分类id
        keywords: "家具家装",   //关键词
        level: 0,           //等级  0 一级分类  1 二级分类
        name: "家具家装",       //名称     
        parentId: 0,        //父级分类id
        productCount: 500,  //数量
        productUnit: "件",   //单位
        showStatus: 0,       //是否显示 0 不显示  1显示
        children:[]
    },
    {
        id: 43,
        keywords: "厨房卫浴",
        level: 1,
        name: "厨房卫浴",
        parentId: 4,
        productCount: 560,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 44,
        keywords: "灯饰照明",
        level: 1,
        name: "灯饰照明",
        parentId: 4,
        productCount: 50,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 45,
        keywords: "五金工具",
        level: 1,
        name: "五金工具",
        parentId: 4,
        productCount: 30,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 46,
        keywords: "卧室家具",
        level: 1,
        name: "卧室家具",
        parentId: 4,
        productCount: 190,
        productUnit: "件",
        showStatus: 0 ,
        children:[]
    },
    {
        id: 47,
        keywords: "客厅家具",
        level: 1,
        name: "客厅家具",
        parentId: 4,
        productCount: 537,
        productUnit: "件",
        showStatus: 0 ,
        children:[]
    },
    {
        id: 5,             //分类id
        keywords: "汽车用品",   //关键词
        level: 0,           //等级  0 一级分类  1 二级分类
        name: "汽车用品",       //名称     
        parentId: 0,        //父级分类id
        productCount: 320,  //数量
        productUnit: "件",   //单位
        showStatus: 0,       //是否显示 0 不显示  1显示
        children:[]
    },
    {
        id: 48,
        keywords: "全新整车",
        level: 1,
        name: "全新整车",
        parentId: 5,
        productCount: 560,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 49,
        keywords: "车载电器",
        level: 1,
        name: "车载电器",
        parentId: 5,
        productCount: 90,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },
    {
        id: 50,
        keywords: "维修保养",
        level: 1,
        name: "维修保养",
        parentId: 5,
        productCount: 130,
        productUnit: "件",
        showStatus: 0 ,
        children:[]
    },
    {
        id: 51,
        keywords: "汽车装饰",
        level: 1,
        name: "汽车装饰",
        parentId: 5,
        productCount: 240,
        productUnit: "件",
        showStatus: 1 ,
        children:[]
    },

]

let _list = [];
_list = allList.filter((item,index)=>item.level==0)
for(let items of _list){
    items.children = allList.filter((item,index)=>item.parentId==items.id)
}

// id,keywords,level,name,parentId,productCount,productUnit,showStatus

//获取所有分类
export const allGoodsList = (data) =>request({
    url:'/allGoodsList',
    method:'get',
    data
})
Mock.mock(/\/allGoodsList/,config=>{
    let {pagesize,pagenum} = JSON.parse(config.body);
    //总条数
    let total = _list.length;
    //返回当前页数的对应的pagesize数量的数据
    if(pagesize && pagenum){
        let user = allList.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                user,total,pagenum,pagesize
            },
            meta:{
                msg:'请求成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
})

// 获取数据列表
export const cGoodsList = (data) =>request({
    url:'/cGoodsList',
    method:'get',
    data
})
Mock.mock(/\/cGoodsList/,config=>{
    let {pagesize,pagenum} = JSON.parse(config.body);
    _list = allList.filter((item,index)=>item.level==0)
    for(let items of _list){
        items.children = allList.filter((item,index)=>item.parentId==items.id)
    }
    //总条数
    let total = _list.length;
    //返回当前页数的对应的pagesize数量的数据
    if(pagesize && pagenum){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                user,total,pagenum,pagesize
            },
            meta:{
                msg:'请求成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
})

//添加分类
export const addCgoods = (data) =>request({
    url:'/addCgoods',
    mothed:'post',
    data
});
Mock.mock(/\/addCgoods/,config=>{
    var ids = Mock.mock({"id|52-99":1000});
    let datas = JSON.parse(config.body)
    let {name,productCount,parentId,level,} = datas;
    if(name && productCount && (parentId || parentId==0) && (level || level==0)){
        if((level==0 && parentId==0) || (level==1 && parentId!=0)){
            allList.unshift({
                level,
                parentId,
                name,
                productCount,
                id:ids.id,
                keywords:datas.keywords?datas.keywords:'',
                productUnit:datas.productUnit?datas.productUnit:'件',
                showStatus:datas.showStatus?datas.showStatus:1
            })
            return {
                meta:{
                    msg:'添加成功',
                    status:200
                }
            }
        }else{
            return {
                meta:{
                    msg:'层级错误',
                    status:400
                }
            }
        }
    }else{
        return {
            meta:{
                msg:'添加失败',
                status:400
            }
        }
    }
    
})

//删除分类
export const detCgoods = (data) =>request({
    url:'/detCgoods',
    mothed:'delete',
    data
})
Mock.mock(/\/detCgoods/,config=>{
    let {id} = JSON.parse(config.body);
    if(id){
        allList.splice(allList.findIndex(item=>item.id==id),1)
        // for(let i in _list){
        //     if(_list[i].id==id){
        //         _list.splice(i,1)
        //     }else{
        //         for(let j in _list[i].children){
        //             if(_list[i].children[j].id==id){
        //                 _list[i].children.splice(j,1)
        //             }
        //         }
        //     }
        // }
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'删除失败,参数错误',
                status:400
            }
        }
    }
    
})

//查询分类 根据关键字
export const queryCgoods = (data) =>request({
    url:'/queryCgoods',
    mothed:'get',
    data
})
Mock.mock(/\/queryCgoods/,config=>{
    let {keywords} = JSON.parse(config.body);
    let arr = [];
    if(keywords){
        arr = allList.filter((item,index)=>item.keywords.indexOf(keywords)>=0)
        // for(let item of _list){
        //     if(item.keywords.indexOf(keywords)>0){
        //         arr.push(item)
        //     }
        //     for(let items of item.children){
        //         if(items.keywords.indexOf(keywords)>0){
        //             arr.push(items)
        //         }
        //     }
        // }
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
})

// id,keywords,level,name,parentId,productCount,productUnit,showStatus
// 修改分类数据
export const editCgoods = (data) =>request({
    url:'/editCgoods',
    mothed:'put',
    data
})
Mock.mock(/\/editCgoods/,config=>{
    let datas = JSON.parse(config.body);
    let {id} = datas;
    if(id){
        for(let item of allList){
            if(item.id==id){
                item.level=datas.level?datas.level:item.level
                item.parentId=datas.parentId?datas.parentId:item.parentId
                item.name = datas.name?datas.name:item.name;
                item.productCount = datas.productCount?datas.productCount:item.productCount;
                item.keywords=datas.keywords?datas.keywords:item.keywords
                item.productUnit=datas.productUnit?datas.productUnit:item.productUnit
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})

//修改是否显示
export const editCgoodsShow = (data) =>request({
    url:'/editCgoodsShow',
    mothed:'put',
    data
})
Mock.mock(/\/editCgoodsShow/,config=>{
    let datas = JSON.parse(config.body);
    let {id,showStatus} = datas;
    if(id){
        for(let item of allList){
            if(item.id==id){
                item.showStatus=showStatus
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})